package interaction.input;

import processing.core.PGraphics;
import processing.core.PVector;
import interaction.drawable.AbstractDrawable;
import interaction.sampling.Sample;

abstract public class AbstractComponent extends AbstractDrawable implements Component {
	
	private PVector position;
	private PVector size;
	
	private boolean hasFocus = false;
	private boolean isHovered = false;
	
	public AbstractComponent(PVector position, PVector size) {
		this.position = position;
		this.size = size;
	}
	
	public PVector getPosition()
	{
		return position;
	}
	
	public PVector getSize() {
		return size;
	}
	
	public void draw(PGraphics g) {
		g.pushMatrix();
		g.translate(position.x, position.y);
		drawRelative(g);
		g.popMatrix();
	}
	
	abstract protected void drawRelative(PGraphics g);
	
	public boolean isOnTop(float mouseX, float mouseY)
	{
		return (mouseX > position.x)
		    && (mouseY > position.y)
			&& (mouseX - size.x < position.x)
			&& (mouseY - size.y < position.y);
	}
	
	public boolean hasFocus()
	{
		return hasFocus;
	}
	
	public AbstractComponent setFocus(boolean hasFocus)
	{
		this.hasFocus = hasFocus;
		return this;
	}
	
	public boolean isHovered()
	{
		return isHovered;
	}
	
	public void keyPressed(char c) {
		if (hasFocus()) {
			handleKeyPress(c);
		}
	}
	
	public void handleKeyPress(char key)
	{
	}
	
	public void mouseMoved(PVector p) {
		isHovered = isOnTop(p.x, p.y);
	}
	
	public void mousePressed(PVector p) {
		hasFocus = isOnTop(p.x, p.y);
	}
	
	public void beatReceived(Sample s)
	{
	}
}
